package com.yzs.exam.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzs.exam.entity.Chapter;
import com.yzs.exam.repository.BaseMapper;
import com.yzs.exam.repository.ChapterMapper;
import com.yzs.exam.service.ChapterService;
import com.yzs.exam.viewmodel.admin.education.ChapterPageRequestVM;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yzs
 * @create 2021-04-22 15:20
 */
@Service
public class ChapterServiceImpl extends BaseServiceImpl<Chapter> implements ChapterService {

    private final static String CACHE_NAME = "xzs:chapter";
    private final ChapterMapper chapterMapper;

    @Autowired
    public ChapterServiceImpl(ChapterMapper chapterMapper) {
        super(chapterMapper);
        this.chapterMapper = chapterMapper;
    }

    //查找后缓存下来
    @Override
    @Cacheable(value = CACHE_NAME, key = "#id", unless = "#result == null")
    public Chapter selectById(Integer id){
        return  super.selectById(id);
    }

    //更新后更新缓存
    @Override
    @CacheEvict(value = CACHE_NAME, key = "#record.id")
    public int updateByIdFilter(Chapter record) {
        return super.updateByIdFilter(record);
    }


    @Override
    public List<Chapter> allChapter() {
        List<Chapter> allChapter = chapterMapper.allChapter();
        return allChapter;
    }


    @Override
    public PageInfo<Chapter> page(ChapterPageRequestVM requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(),requestVM.getPageSize(),"id desc").doSelectPageInfo(() ->{
            chapterMapper.page(requestVM);
        });
    }

    @Override
    public List<Chapter> getChaptersByLevel(Integer level) {
        return chapterMapper.getChaptersByLevel(level);
    }
}
